﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Reflection;
using Microsoft.Win32;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Timers;

namespace keyBoardEmulate
{
    
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //勾子管理类
        private KeyboardHookLib _keyboardHook = null;
        System.Timers.Timer aTimer = new System.Timers.Timer();
        bool vitualClock = false;
        int clockCnt = 0;

        #region bVk参数 常量定义

        public const byte vbKeyLButton = 0x1;    // 鼠标左键
        public const byte vbKeyRButton = 0x2;    // 鼠标右键
        public const byte vbKeyCancel = 0x3;     // CANCEL 键
        public const byte vbKeyMButton = 0x4;    // 鼠标中键
        public const byte vbKeyBack = 0x8;       // BACKSPACE 键
        public const byte vbKeyTab = 0x9;        // TAB 键
        public const byte vbKeyClear = 0xC;      // CLEAR 键
        public const byte vbKeyReturn = 0xD;     // ENTER 键
        public const byte vbKeyShift = 0x10;     // SHIFT 键
        public const byte vbKeyControl = 0x11;   // CTRL 键
        public const byte vbKeyAlt = 18;         // Alt 键  (键码18)
        public const byte vbKeyMenu = 0x12;      // MENU 键
        public const byte vbKeyPause = 0x13;     // PAUSE 键
        public const byte vbKeyCapital = 0x14;   // CAPS LOCK 键
        public const byte vbKeyEscape = 0x1B;    // ESC 键
        public const byte vbKeySpace = 0x20;     // SPACEBAR 键
        public const byte vbKeyPageUp = 0x21;    // PAGE UP 键
        public const byte vbKeyEnd = 0x23;       // End 键
        public const byte vbKeyHome = 0x24;      // HOME 键
        public const byte vbKeyLeft = 0x25;      // LEFT ARROW 键
        public const byte vbKeyUp = 0x26;        // UP ARROW 键
        public const byte vbKeyRight = 0x27;     // RIGHT ARROW 键
        public const byte vbKeyDown = 0x28;      // DOWN ARROW 键
        public const byte vbKeySelect = 0x29;    // Select 键
        public const byte vbKeyPrint = 0x2A;     // PRINT SCREEN 键
        public const byte vbKeyExecute = 0x2B;   // EXECUTE 键
        public const byte vbKeySnapshot = 0x2C;  // SNAPSHOT 键
        public const byte vbKeyDelete = 0x2E;    // Delete 键
        public const byte vbKeyHelp = 0x2F;      // HELP 键
        public const byte vbKeyNumlock = 0x90;   // NUM LOCK 键

        //常用键 字母键A到Z
        public const byte vbKeyA = 65;
        public const byte vbKeyB = 66;
        public const byte vbKeyC = 67;
        public const byte vbKeyD = 68;
        public const byte vbKeyE = 69;
        public const byte vbKeyF = 70;
        public const byte vbKeyG = 71;
        public const byte vbKeyH = 72;
        public const byte vbKeyI = 73;
        public const byte vbKeyJ = 74;
        public const byte vbKeyK = 75;
        public const byte vbKeyL = 76;
        public const byte vbKeyM = 77;
        public const byte vbKeyN = 78;
        public const byte vbKeyO = 79;
        public const byte vbKeyP = 80;
        public const byte vbKeyQ = 81;
        public const byte vbKeyR = 82;
        public const byte vbKeyS = 83;
        public const byte vbKeyT = 84;
        public const byte vbKeyU = 85;
        public const byte vbKeyV = 86;
        public const byte vbKeyW = 87;
        public const byte vbKeyX = 88;
        public const byte vbKeyY = 89;
        public const byte vbKeyZ = 90;

        //数字键盘0到9
        public const byte vbKey0 = 48;    // 0 键
        public const byte vbKey1 = 49;    // 1 键
        public const byte vbKey2 = 50;    // 2 键
        public const byte vbKey3 = 51;    // 3 键
        public const byte vbKey4 = 52;    // 4 键
        public const byte vbKey5 = 53;    // 5 键
        public const byte vbKey6 = 54;    // 6 键
        public const byte vbKey7 = 55;    // 7 键
        public const byte vbKey8 = 56;    // 8 键
        public const byte vbKey9 = 57;    // 9 键


        public const byte vbKeyNumpad0 = 0x60;    //0 键
        public const byte vbKeyNumpad1 = 0x61;    //1 键
        public const byte vbKeyNumpad2 = 0x62;    //2 键
        public const byte vbKeyNumpad3 = 0x63;    //3 键
        public const byte vbKeyNumpad4 = 0x64;    //4 键
        public const byte vbKeyNumpad5 = 0x65;    //5 键
        public const byte vbKeyNumpad6 = 0x66;    //6 键
        public const byte vbKeyNumpad7 = 0x67;    //7 键
        public const byte vbKeyNumpad8 = 0x68;    //8 键
        public const byte vbKeyNumpad9 = 0x69;    //9 键
        public const byte vbKeyMultiply = 0x6A;   // MULTIPLICATIONSIGN(*)键
        public const byte vbKeyAdd = 0x6B;        // PLUS SIGN(+) 键
        public const byte vbKeySeparator = 0x6C;  // ENTER 键
        public const byte vbKeySubtract = 0x6D;   // MINUS SIGN(-) 键
        public const byte vbKeyDecimal = 0x6E;    // DECIMAL POINT(.) 键
        public const byte vbKeyDivide = 0x6F;     // DIVISION SIGN(/) 键


        //F1到F12按键
        public const byte vbKeyF1 = 0x70;   //F1 键
        public const byte vbKeyF2 = 0x71;   //F2 键
        public const byte vbKeyF3 = 0x72;   //F3 键
        public const byte vbKeyF4 = 0x73;   //F4 键
        public const byte vbKeyF5 = 0x74;   //F5 键
        public const byte vbKeyF6 = 0x75;   //F6 键
        public const byte vbKeyF7 = 0x76;   //F7 键
        public const byte vbKeyF8 = 0x77;   //F8 键
        public const byte vbKeyF9 = 0x78;   //F9 键
        public const byte vbKeyF10 = 0x79;  //F10 键
        public const byte vbKeyF11 = 0x7A;  //F11 键
        public const byte vbKeyF12 = 0x7B;  //F12 键

        #endregion
        static public bool start_battle = false;
        static public bool 八重樱_巫女_开启大招 = false;
        static public bool 八重樱_勿忘_释放分支 = false;
        static BattleCommon btC = new BattleCommon();
        static Thread sendKeyString_thread   = new Thread(new ThreadStart(sendKeyString));


        #region 引用win32api方法

        /// <summary>
        /// 导入模拟键盘的方法
        /// </summary>
        /// <param name="bVk" >按键的虚拟键值</param>
        /// <param name= "bScan" >扫描码，一般不用设置，用0代替就行</param>
        /// <param name= "dwFlags" >选项标志：0：表示按下，2：表示松开</param>
        /// <param name= "dwExtraInfo">一般设置为0</param>
        [DllImport("user32.dll")]
        public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
        [DllImport("user32.dll")]
        private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
        [DllImport("user32.dll")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);
        [DllImport("user32.dll")]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [DllImport("user32.dll")]
        private static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
        [DllImport("user32.dll")]
        private static extern bool SetCursorPos(int X, int Y);
        [DllImport("user32.dll")]
        private static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
        [DllImport("user32.dll")]
        private static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);
        [DllImport("user32.dll")]
        private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndlnsertAfter, int X, int Y, int cx, int cy, uint Flags);
        #endregion
        //ShowWindow参数
        private const int SW_SHOWNORMAL = 1;
        private const int SW_RESTORE = 9;
        private const int SW_SHOWNOACTIVATE = 4;
        //SendMessage参数
        private const int WM_KEYDOWN = 0X100;
        private const int WM_KEYUP = 0X101;
        private const int WM_SYSCHAR = 0X106;
        private const int WM_SYSKEYUP = 0X105;
        private const int WM_SYSKEYDOWN = 0X104;
        private const int WM_CHAR = 0X102;

        [DllImport("user32.dll")]
        static extern IntPtr SetActiveWindow(IntPtr hWnd);
        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        public static extern IntPtr GetForegroundWindow();
        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int nMaxCount);
        [DllImport("user32.dll")]
        public static extern int GetWindowTextLength(IntPtr hWnd);


        private void button1_Click(object sender, EventArgs e)
        {

            
            sendKeyString_thread.Start();
            
            
            sendInfo("已经开始发送");
        }
        public void sendInfo(String text)
        {
            richTextBox1.Text += text + "\n";
        }
        public void installHook()
        {

            //安装勾子
            _keyboardHook = new KeyboardHookLib();
            _keyboardHook.InstallHook(this.KeyPress);


        }
        public void uninstallHook()
        {
            //取消勾子
            if (_keyboardHook != null) _keyboardHook.UninstallHook();
        }

        /// <summary>
        /// 客户端键盘捕捉事件.
        /// </summary>
        /// <param name="hookStruct">由Hook程序发送的按键信息</param>
        /// <param name="handle">是否拦截</param>
        public new void KeyPress(KeyboardHookLib.HookStruct hookStruct, out bool handle)
        {
            handle = false; //预设不拦截任何键                   
            
            //截获z
            if (hookStruct.vkCode == (int)Keys.R )
            {
                
                handle = true;
                
                start_battle = !start_battle;
               
            }
            else if (hookStruct.vkCode == (int)Keys.C )
            {

                handle = true;
                start_battle = true;
                //sendKeyString_thread.Interrupt();
                //sendKeyString_thread.Abort();
                //if (sendKeyString_thread != null)
                //{
                //    //sendKeyString_thread = new Thread(new ThreadStart(sendKeyString));
                //    sendKeyString_thread.Resume();
                //}
                


            }
            else if (hookStruct.vkCode == (int)Keys.X && (int)Control.ModifierKeys == (int)Keys.Alt)
            {

                handle = true;
                start_battle = true;
               
                
                //sendKeyString_thread = new Thread(new ThreadStart(sendKeyString));
                //sendKeyString_thread.Start();
                



            }
            if (hookStruct.vkCode == (int)Keys.V )
            {
                handle = false;
                
                btC.btType = BattleCommon.BattleType.符华_白夜;

                
                handle = true;

            }
            if (hookStruct.vkCode == (int)Keys.B)
            {

                handle = false;
                btC.btType = BattleCommon.BattleType.八重樱_真炎;
                btC.battleType = comboBox1.Text;
                label5.Text = "当前配置：" + comboBox1.Text + "(B)";
                handle = true;

            }
            if (hookStruct.vkCode == (int)Keys.N)
            {

                handle = false;
                btC.btType = BattleCommon.BattleType.八重樱_勿忘;
                btC.battleType = comboBox2.Text;
                label5.Text = "当前配置：" + comboBox2.Text + "(N)";
                handle = true;

            }
            if (hookStruct.vkCode == (int)Keys.M)
            {

                handle = false;
                btC.btType = BattleCommon.BattleType.八重樱_巫女;
                //BattleCommon.sendKeyWithKeyDownDelay((byte)Keys.R, 20);
                btC.battleType = comboBox3.Text;
                label5.Text = "当前配置：" + comboBox3.Text + "(M)";
                handle = true;

            }
            //if (hookStruct.vkCode == (int)Keys.L)
            //{
            //    handle = false;
            //    if(btC.btType == BattleCommon.BattleType.八重樱_巫女)
            //        八重樱_巫女_开启大招 = true;


            //}
            if (hookStruct.vkCode == (int)Keys.H)
            {
                handle = false;
                if (btC.btType == BattleCommon.BattleType.八重樱_勿忘)
                    八重樱_勿忘_释放分支 = true;


            }





            //如果键1-2
            if (hookStruct.vkCode == (int)Keys.D1 || hookStruct.vkCode == (int)Keys.D2)
            {
                //挡掉G键，想要挡掉哪些键就把下面的G换成那个要挡掉的键，同理可以挡多个
                if (hookStruct.vkCode == (int)Keys.G)
                    hookStruct.vkCode = (int)Keys.None; //设键为0

                //handle = true;
            }

            Keys key = (Keys)hookStruct.vkCode;
            label1.Text = "你按下:" + (key == Keys.None ? "" : key.ToString());
            Console.WriteLine(label1.Text);
        }
 public void test(object source, System.Timers.ElapsedEventArgs e)
        {
            //MessageBox.Show(DateTime.Now.ToString());
            vitualClock = !vitualClock;
            Console.WriteLine(vitualClock.ToString());
        }

        

        public void test_TimerEvent()
        {
            
            Console.WriteLine(vitualClock.ToString());
            while (true)
               
            {
                if (vitualClock == true)
                {
                    clockCnt = clockCnt + 1;
                }
                if (clockCnt == 1001)
                {
                   
                    clockCnt = 0;


                }

                if (clockCnt %500== 0)
                {
                    
                    


                }
            }
        }
 static public void sendKeyString()
        {
            while (true)
            {
                

                

                //try {

                //    if (start_battle == false)
                //       Thread.Sleep(Timeout.Infinite);

                    IntPtr hWnd = GetForegroundWindow();
                    int length = GetWindowTextLength(hWnd);
                    StringBuilder windowName = new StringBuilder(length + 1);
                    GetWindowText(hWnd, windowName, windowName.Capacity);
                Console.WriteLine(windowName.ToString());
                // Thread.Sleep(1000);
                //Console.WriteLine("发送线程运行中!");

                Console.WriteLine(btC.battleType);
                if (start_battle == true && windowName.ToString() == "崩坏3 - MuMu模拟器")
                    {
                    
                    //SendKeys.SendWait("J");
                    //模拟按下J键
                    //keybd_event(vbKeyJ, 0, 0, 0);
                    //Thread.Sleep(500);
                    //松开按键J
                    //keybd_event(vbKeyJ, 0, 2, 0);

                    switch (btC.battleType)
                        {

                            case ("符华-白夜"):
                                {

                                    BattleCommon.符华_白夜_Attak();
                                }
                                break;
                        case ("符华-迅羽"):
                            {

                                BattleCommon.符华_迅羽_连招();
                            }
                            break;
                        case ("八重樱-真炎"):
                                {
                                    BattleCommon.SeqAttak(4, 100);
                                    Thread.Sleep(500);
                                    BattleCommon.SeqAttak(1);



                                }
                                break;
                            case ("八重樱-勿忘"):
                                {
                                    if(八重樱_勿忘_释放分支)
                                    {
                                        BattleCommon.八重樱_勿忘_释放分支_Attack();
                                        八重樱_勿忘_释放分支 = false;
                                    }
                                    else 
                                        BattleCommon.八重樱_勿忘_Attack();
                                }
                                break;
                            case ("八重樱-巫女"):
                                {
                                    if (八重樱_巫女_开启大招)
                                    {
                                        BattleCommon.八重樱_巫女_大招状态();
                                        八重樱_巫女_开启大招 = false;
                                    }
                                    else
                                        BattleCommon.八重樱_巫女_Attack();
                                }
                                break;
                             case ("无量塔姬子-凯旋"):
                                {
                                    BattleCommon.凯旋_蓄力攻击();
                                }
                                break;

                            case ("琪亚娜-白练"):

                   
                            {
                                BattleCommon.SeqAttak(5);
                            }
                            break;
                            default:
                                break;

                        }
                    }

                //}
                        
                //catch(ThreadInterruptedException)
                //{
                //   Console.WriteLine("Thread '{0}' awoken.",
                //       Thread.CurrentThread.Name);
                //}
                //catch (ThreadAbortException)
                //{
                //   Console.WriteLine("Thread '{0}' aborted.",
                //                     Thread.CurrentThread.Name);
                //}
                
                

                
            }
        }
        

        private void button2_Click(object sender, EventArgs e)
        {
            start_battle = false;
            sendInfo("开始停止发送");
        }

        
        

        

        private void Form1_Load(object sender, EventArgs e)
        {
            //_keyboardHook = new KeyboardHookLib();
            //_keyboardHook.InstallHook(this.KeyPress);
           // sendKeyString_thread = new Thread(new ThreadStart(sendKeyString));
            
            installHook();

            ValkyrieInfoList infoList1 = new ValkyrieInfoList();
            ValkyrieInfoList infoList2 = new ValkyrieInfoList();
            ValkyrieInfoList infoList3 = new ValkyrieInfoList();

            comboBox1.DataSource = infoList1.getValkyrieInfoList();
            comboBox1.ValueMember = "Id";
            comboBox1.DisplayMember = "Name";
            comboBox2.DataSource = infoList2.getValkyrieInfoList();
            comboBox2.ValueMember = "Id";
            comboBox2.DisplayMember = "Name";
            comboBox3.DataSource = infoList2.getValkyrieInfoList();
            comboBox3.ValueMember = "Id";
            comboBox3.DisplayMember = "Name";

            comboBox1.SelectedIndex = 26-1;
            comboBox2.SelectedIndex = 31 - 1;

        }
        public class ValkyrieInfo
        {
            public string Id { get; set; }
            public string Name { get; set; }

        }
        public class ValkyrieInfoList
        {
            
            //kiana
            ValkyrieInfo info1 = new ValkyrieInfo() { Id = "1", Name = "琪亚娜-圣女" };
            ValkyrieInfo info2 = new ValkyrieInfo() { Id = "2", Name = "琪亚娜-白练" };
            ValkyrieInfo info3 = new ValkyrieInfo() { Id = "3", Name = "琪亚娜-月光" };
            ValkyrieInfo info4 = new ValkyrieInfo() { Id = "4", Name = "琪亚娜-游侠" };

            //kallen
            ValkyrieInfo info1_1 = new ValkyrieInfo() { Id = "5", Name = "卡莲-今样" };
            ValkyrieInfo info2_1 = new ValkyrieInfo() { Id = "6", Name = "卡莲-第六夜想曲" };
            ValkyrieInfo info3_1 = new ValkyrieInfo() { Id = "7", Name = "卡莲-原罪猎人" };
            //ValkyrieInfo info4_1 = new ValkyrieInfo() { Id = "8", Name = "琪亚娜-游侠" };

            //mei
            ValkyrieInfo info5 = new ValkyrieInfo() { Id = "8", Name = "芽衣-绯红" };
            ValkyrieInfo info6 = new ValkyrieInfo() { Id = "9", Name = "芽衣-影舞" };
            ValkyrieInfo info7 = new ValkyrieInfo() { Id = "10", Name = "芽衣-鬼铠" };
            ValkyrieInfo info8 = new ValkyrieInfo() { Id = "11", Name = "芽衣-强袭" };

            //yae
            ValkyrieInfo info9 = new ValkyrieInfo() { Id = "12", Name = "八重樱-巫女" };
            ValkyrieInfo info10 = new ValkyrieInfo() { Id = "13", Name = "八重樱-勿忘" };
            ValkyrieInfo info11 = new ValkyrieInfo() { Id = "14", Name = "八重樱-真炎" };

            //banya
            ValkyrieInfo info12 = new ValkyrieInfo() { Id = "15", Name = "布洛尼娅-战车" };
            ValkyrieInfo info13 = new ValkyrieInfo() { Id = "16", Name = "布洛尼娅-山吹" };
            ValkyrieInfo info14 = new ValkyrieInfo() { Id = "17", Name = "布洛尼娅-银狼" };
            ValkyrieInfo info15 = new ValkyrieInfo() { Id = "18", Name = "布洛尼娅-次元" };
            ValkyrieInfo info16 = new ValkyrieInfo() { Id = "19", Name = "布洛尼娅-雪地" };
            ValkyrieInfo info17 = new ValkyrieInfo() { Id = "20", Name = "布洛尼娅-黑核" };

            //DaYiMa
            ValkyrieInfo info18 = new ValkyrieInfo() { Id = "21", Name = "德丽莎-誓约" };
            ValkyrieInfo info19 = new ValkyrieInfo() { Id = "22", Name = "德丽莎-月下" };
            ValkyrieInfo info20 = new ValkyrieInfo() { Id = "23", Name = "德丽莎-樱火" };
            ValkyrieInfo info21 = new ValkyrieInfo() { Id = "24", Name = "德丽莎-紫苑" };
            ValkyrieInfo info22 = new ValkyrieInfo() { Id = "25", Name = "德丽莎-神恩" };

            //himeko
            ValkyrieInfo info23 = new ValkyrieInfo() { Id = "26", Name = "无量塔姬子-凯旋" };
            ValkyrieInfo info24 = new ValkyrieInfo() { Id = "27", Name = "无量塔姬子-深红" };
            ValkyrieInfo info25 = new ValkyrieInfo() { Id = "28", Name = "无量塔姬子-疾风" };
            ValkyrieInfo info26 = new ValkyrieInfo() { Id = "29", Name = "无量塔姬子-极地" };
            ValkyrieInfo info27 = new ValkyrieInfo() { Id = "30", Name = "无量塔姬子-玫瑰" };

            //fuhua
            ValkyrieInfo info28 = new ValkyrieInfo() { Id = "31", Name = "符华-迅羽" };
            ValkyrieInfo info29 = new ValkyrieInfo() { Id = "32", Name = "符华-白夜" };
            ValkyrieInfo info30 = new ValkyrieInfo() { Id = "33", Name = "符华-炽翎" };
            ValkyrieInfo info31 = new ValkyrieInfo() { Id = "34", Name = "符华-月轮" };
            public IList<ValkyrieInfo> getValkyrieInfoList()
            {
                IList<ValkyrieInfo> infoList = new List<ValkyrieInfo>();
                infoList.Add(info1);
                infoList.Add(info2);
                infoList.Add(info3);
                infoList.Add(info4);
                infoList.Add(info1_1);
                infoList.Add(info2_1);
                infoList.Add(info3_1);

                infoList.Add(info5);
                infoList.Add(info6);
                infoList.Add(info7);
                infoList.Add(info8);
                infoList.Add(info9);
                infoList.Add(info10);
                infoList.Add(info11);
                infoList.Add(info12);
                infoList.Add(info13);
                infoList.Add(info14);
                infoList.Add(info15);
                infoList.Add(info16);
                infoList.Add(info17);
                infoList.Add(info18);
                infoList.Add(info19);
                infoList.Add(info20);
                infoList.Add(info21);
                infoList.Add(info22);
                infoList.Add(info23);
                infoList.Add(info24);
                infoList.Add(info25);
                infoList.Add(info26);
                infoList.Add(info27);
                infoList.Add(info28);
                infoList.Add(info29);
                infoList.Add(info30);
                infoList.Add(info31);

                return infoList;
            }
            
        }
        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (!((RadioButton)sender).Checked)
            {
                return;
            }
            switch (((RadioButton)sender).Text.ToString())
            {
                case "符华-白夜":
                    btC.btType = BattleCommon.BattleType.符华_白夜;
                    break;

                default:
                  
                    break;

            }
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            //switch (e.KeyCode)
            //{
            //    case Keys.S:
            //        start_battle = false;
            //        break;

            //    case Keys.C:
            //        start_battle = true;
            //        break;
            //}
            //if (start_battle)
            //    label1.Text = "发送状态：发送中";
            //else
            //    label1.Text = "发送状态：停止";
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            sendKeyString_thread.Abort();
            uninstallHook();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            uninstallHook();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            aTimer.Elapsed += new ElapsedEventHandler(test);
            aTimer.Interval = 1000;
            aTimer.AutoReset = true;//执行一次 false，一直执行true  
                                    //是否执行System.Timers.Timer.Elapsed事件  
            aTimer.Enabled = true;
        }

        private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            
            btC.slot1_女武神名称 = comboBox1.Text;
            btC.slot2_女武神名称 = comboBox2.Text;
            btC.slot3_女武神名称 = comboBox3.Text;

            //sendInfo("-------------");
            //sendInfo(comboBox1.Text);
            //sendInfo(comboBox2.Text);
            //sendInfo(comboBox3.Text);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            sendKeyString_thread = new Thread(new ThreadStart(test_TimerEvent));
            sendKeyString_thread.Start();
        }

        private void label5_Click(object sender, EventArgs e)
        {

        }
    }



    public class BattleCommon
    {
        public string slot1_女武神名称, slot2_女武神名称, slot3_女武神名称;
        public enum BattleType
        {
            符华_白夜,
            八重樱_真炎,
            八重樱_勿忘,
            八重樱_巫女,
        }
        public BattleType btType;
        public string battleType;
        static public int deLay1 = 150;
        static public int deLay2 = 200;
        static public int deLay3 = 100;

        [DllImport("user32.dll")]
        public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

        public static void Delay(int milliSecond)
        {
            int start = Environment.TickCount;
            while (Math.Abs(Environment.TickCount - start) < milliSecond && Form1.start_battle == true)//毫秒
            {
                Application.DoEvents();//可执行某无聊的操作
            }
           
            
        }
        static public void sendKeyWithKeyDownDelay(byte key, int keyDownTime_ms)
        {
            if(Form1.start_battle)
            {
                keybd_event(key, 0, 0, 0);
                Delay(keyDownTime_ms); //key down time
                keybd_event(key, 0, 2, 0);
            }
            
        }
        static public void SeqAttak(int times)
        {
            for (int k = 0; k < times; k++)
            {
                sendKeyWithKeyDownDelay((byte)Keys.K, 30);
                Delay(deLay1);
            }
           
        }
        static public void SeqAttak(int times, int delay)
        {
            for (int k = 0; k < times; k++)
            {
                sendKeyWithKeyDownDelay((byte)Keys.K, 30);
                Delay(delay);
            }

        }
        static public void 符华_迅羽_连招()
        {
            迅羽_寸劲_岩破();
            迅羽_寸劲_云岚();
        }

        static public void 迅羽_寸劲_岩破()
        {
            SeqAttak(1, 200);
            sendKeyWithKeyDownDelay((byte)Keys.L, 30);
            Delay(200);
            sendKeyWithKeyDownDelay((byte)Keys.L, 30);
            Delay(200);
        }

        static public void 迅羽_寸劲_云岚()
        {
            SeqAttak(4, 200);
            sendKeyWithKeyDownDelay((byte)Keys.L, 30);
            Delay(200);
            sendKeyWithKeyDownDelay((byte)Keys.L, 30);
            Delay(200);
            sendKeyWithKeyDownDelay((byte)Keys.L, 30);
            Delay(200);
        }
        static public void 迅羽_寸劲_乱雷()
        {

        }
        static public void 迅羽_寸劲_霹雳()
        {

        }
        

        static public void 符华_白夜_Attak()
        {
            BattleCommon.SeqAttak(3);
            BattleCommon.YueXiaWu();

            BattleCommon.HuangXiangjiao();
            BattleCommon.RuYunYing();

            BattleCommon.SeqAttak(3);
            BattleCommon.TengKongQue();

            BattleCommon.SeqAttak(3);
            BattleCommon.TengKongQue();

            BattleCommon.SeqAttak(3);
            BattleCommon.HuangXiangjiao();
            BattleCommon.YueXiaWu();
            BattleCommon.XuanNiaoSan();

            BattleCommon.SeqAttak(3);
            BattleCommon.HuangXiangjiao();
            BattleCommon.YueXiaWu();
            BattleCommon.YueXiaWu();

            BattleCommon.SeqAttak(3);
            BattleCommon.SeqAttak(3);
            BattleCommon.RuYunYing();
            BattleCommon.YueXiaWu();
        }
        static public void 八重樱_勿忘_Attack()
        {
            //50次普通攻击之后释放分支
            for (int i = 0; i < 5; i++)
            {
                BattleCommon.SeqAttak(5, 200);
            }
            

        }
        static public void 八重樱_勿忘_释放分支_Attack()
        {
            八重樱_勿忘_Attack_分支(6);
        }
        static public void 八重樱_巫女_Attack()
        {
            
            //50次普通攻击之后释放分支
            for (int i = 0; i < 5; i++)
            {
                BattleCommon.SeqAttak(5, 200);
            }
            八重樱_巫女_Attack_分支(1);

        }
        static public void 八重樱_巫女_大招状态()
        {

            Delay(3000);
            for (int i = 0; i < 3; i++)
            {
                BattleCommon.SeqAttak(4, 200);
                BattleCommon.sendKeyWithKeyDownDelay((byte)Keys.K, 1000);
                Delay(100);
            }

        }
        static public void 八重樱_勿忘_Attack_分支(int times)
        {
            
            BattleCommon.sendKeyWithKeyDownDelay((byte)Keys.J, 20);
            Delay(200);
            
            //1
            BattleCommon.sendKeyWithKeyDownDelay((byte)Keys.K, 1000);
            Delay(100);
            //2
            BattleCommon.sendKeyWithKeyDownDelay((byte)Keys.K, 100);
            Delay(100);
            //3
            BattleCommon.sendKeyWithKeyDownDelay((byte)Keys.K, 100);
            Delay(100);
            //4
            BattleCommon.sendKeyWithKeyDownDelay((byte)Keys.K, 100);
            Delay(100);
            //5
            BattleCommon.sendKeyWithKeyDownDelay((byte)Keys.K, 100);
            Delay(100);
            //6
            BattleCommon.sendKeyWithKeyDownDelay((byte)Keys.K, 100);
            Delay(100);



        }
        static public void 八重樱_巫女_Attack_分支(int times)
        {

            BattleCommon.sendKeyWithKeyDownDelay((byte)Keys.J, 20);
            Delay(200);
            for (int k = 0; k < times; k++)
            {
                
                BattleCommon.sendKeyWithKeyDownDelay((byte)Keys.K, 1000);
            }


        }
        static public void HuangXiangjiao()
        {
           
            BattleCommon.sendKeyWithKeyDownDelay((byte)Keys.L, 20);
            Delay(deLay1);
           
            BattleCommon.sendKeyWithKeyDownDelay((byte)Keys.L, 20);
            Delay(deLay2);
            
            BattleCommon.sendKeyWithKeyDownDelay((byte)Keys.L, 20);
            Delay(deLay3);
        }
        static public void RuYunYing()
        {
            BattleCommon.sendKeyWithKeyDownDelay((byte)Keys.K, 20);
            Delay(deLay1);
            BattleCommon.sendKeyWithKeyDownDelay((byte)Keys.K, 20);
            Delay(deLay2);
            BattleCommon.sendKeyWithKeyDownDelay((byte)Keys.L, 20);
            Delay(deLay3);
        }
        static public void XuanNiaoSan()
        {
            BattleCommon.sendKeyWithKeyDownDelay((byte)Keys.K, 20);
            Delay(deLay1);
            BattleCommon.sendKeyWithKeyDownDelay((byte)Keys.L, 20);
            Delay(deLay2);
            BattleCommon.sendKeyWithKeyDownDelay((byte)Keys.L, 20);
            Delay(deLay3);
        }
        static public void TengKongQue()
        {
            
            BattleCommon.sendKeyWithKeyDownDelay((byte)Keys.L, 20);
            Delay(deLay1);
            BattleCommon.sendKeyWithKeyDownDelay((byte)Keys.L, 20);
            Delay(deLay2);
            BattleCommon.sendKeyWithKeyDownDelay((byte)Keys.K, 20);
            Delay(deLay3);
        }
        static public void YueXiaWu()
        {
            BattleCommon.sendKeyWithKeyDownDelay((byte)Keys.L, 20);
            Delay(deLay1);
            BattleCommon.sendKeyWithKeyDownDelay((byte)Keys.K, 20);
            Delay(deLay2);
            BattleCommon.sendKeyWithKeyDownDelay((byte)Keys.K, 20);
            Delay(deLay3);
            BattleCommon.sendKeyWithKeyDownDelay((byte)Keys.K, 20);
            Delay(deLay3);
            BattleCommon.sendKeyWithKeyDownDelay((byte)Keys.K, 20);
            Delay(deLay3);
        }

        //himeko
        static public void 凯旋_蓄力攻击()
        {
            //BattleCommon.sendKeyWithKeyDownDelay((byte)Keys.J, 20);

            BattleCommon.sendKeyWithKeyDownDelay((byte)Keys.K, 2000);
           
            BattleCommon.sendKeyWithKeyDownDelay((byte)Keys.K, 2000);
            Delay(50);
            BattleCommon.sendKeyWithKeyDownDelay((byte)Keys.K, 1800);


            
            //BattleCommon.sendKeyWithKeyDownDelay((byte)Keys.K, 2500);
            //Delay(200);
            //BattleCommon.sendKeyWithKeyDownDelay((byte)Keys.K, 20);
            //Delay(150);
            //BattleCommon.sendKeyWithKeyDownDelay((byte)Keys.K, 20);

            //Delay(200);

        }
        static public void 普通攻击()
        {
            BattleCommon.sendKeyWithKeyDownDelay((byte)Keys.L, 20);
            Delay(deLay1);
            BattleCommon.sendKeyWithKeyDownDelay((byte)Keys.L, 20);
            Delay(deLay1);
            BattleCommon.sendKeyWithKeyDownDelay((byte)Keys.L, 20);
            Delay(deLay1);
            BattleCommon.sendKeyWithKeyDownDelay((byte)Keys.L, 20);
            Delay(deLay1);
            BattleCommon.sendKeyWithKeyDownDelay((byte)Keys.L, 20);
            Delay(deLay1);
        }

    }

   


    /// <summary>
    /// 键盘Hook管理类

    /// </summary>
    public class KeyboardHookLib
    {
        private const int WH_KEYBOARD_LL = 13; //键盘

        //键盘处理事件委托.
        private delegate int HookHandle(int nCode, int wParam, IntPtr lParam);

        //客户端键盘处理事件
        public delegate void ProcessKeyHandle(HookStruct param, out bool handle);

        //接收SetWindowsHookEx返回值
        private static int _hHookValue = 0;

        //勾子程序处理事件
        private HookHandle _KeyBoardHookProcedure;
        private const int WM_KEYUP = 0x101;     //按键抬起
        private const int WM_KEYDOWN = 0x100;       //按键按下
        //Hook结构
        [StructLayout(LayoutKind.Sequential)]
        public class HookStruct
        {
            public int vkCode;
            public int scanCode;
            public int flags;
            public int time;
            public int dwExtraInfo;
        }

        //设置钩子
        [DllImport("user32.dll")]
        private static extern int SetWindowsHookEx(int idHook, HookHandle lpfn, IntPtr hInstance, int threadId);

        //取消钩子
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        private static extern bool UnhookWindowsHookEx(int idHook);

        //调用下一个钩子
        [DllImport("user32.dll")]
        private static extern int CallNextHookEx(int idHook, int nCode, int wParam, IntPtr lParam);

        //获取当前线程ID
        [DllImport("kernel32.dll")]
        private static extern int GetCurrentThreadId();

        //Gets the main module for the associated process.
        [DllImport("kernel32.dll")]
        private static extern IntPtr GetModuleHandle(string name);

        private IntPtr _hookWindowPtr = IntPtr.Zero;

        //构造器
        public KeyboardHookLib() { }

        //外部调用的键盘处理事件
        private static ProcessKeyHandle _clientMethod = null;

        /// <summary>
        /// 安装勾子
        /// </summary>
        /// <param name="hookProcess">外部调用的键盘处理事件</param>
        public void InstallHook(ProcessKeyHandle clientMethod)
        {
            _clientMethod = clientMethod;

            // 安装键盘钩子
            if (_hHookValue == 0)
            {
                _KeyBoardHookProcedure = new HookHandle(GetHookProc);

                _hookWindowPtr = GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName);

                _hHookValue = SetWindowsHookEx(
                    WH_KEYBOARD_LL,
                    _KeyBoardHookProcedure,
                    _hookWindowPtr,
                    0);

                //如果设置钩子失败.
                if (_hHookValue == 0)

                    UninstallHook();
            }
        }

        //取消钩子事件
        public void UninstallHook()
        {
            if (_hHookValue != 0)
            {
                bool ret = UnhookWindowsHookEx(_hHookValue);
                if (ret) _hHookValue = 0;
            }
        }

        //钩子事件内部调用,调用_clientMethod方法转发到客户端应用。
        private static int GetHookProc(int nCode, int wParam, IntPtr lParam)
        {
            if (nCode >= 0)
            {
                //转换结构
                HookStruct hookStruct = (HookStruct)Marshal.PtrToStructure(lParam, typeof(HookStruct));

                if (_clientMethod != null)
                {
                    bool handle = false;
                    //调用客户提供的事件处理程序。
                    if (wParam == WM_KEYUP)
                        _clientMethod(hookStruct, out handle);
                    if (handle) return 1; //1:表示拦截键盘,return 退出
                }
            }
            return CallNextHookEx(_hHookValue, nCode, wParam, lParam);
        }

    }
}
